/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tos.pip;

import org.jdesktop.application.FrameView;
import tos.TosView;

/**
 *
 * @author Milos
 */
public class Potrosac extends Thread {

    private static int ukId = 0;
    private int id;
    private Skladiste skladiste;
    private int minVreme, maxVreme;
    private FrameView parent;

    public Potrosac(Skladiste skladiste, int minVreme, int maxVreme, FrameView parent) {
        this.skladiste = skladiste;
        this.minVreme = minVreme;
        this.maxVreme = maxVreme;
        id = ++ukId;
        this.parent = parent;
    }

    public void run() {
        System.out.println("Potrosac " + id + " krenuo.");
        ((TosView) parent).appendOutput("Potrosac " + id + " krenuo.\n");
        try {
            while (true) {
                boolean exit = false;
                if (((TosView) parent).zaustaviPip) {
                    exit = true;
                }
                int proizvod = skladiste.uzmi(exit);
                // Break when skladiste empty and zaustiPit true.
                if (((TosView) parent).zaustaviPip && proizvod == -1) {
                    break;
                }
                
                System.out.println("Potrosac " + id + " uzeo " + proizvod);
                ((TosView) parent).appendOutput("Potrosac " + id + " uzeo " + proizvod + "\n");
                sleep((long) (minVreme + Math.random() * (maxVreme - minVreme)));
            }
            ((TosView) parent).appendOutput("Potrosac " + id + " je stao.\n");
        } catch (InterruptedException ir) {
        }
    }
}
